package com.fang.apg;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.util.Scanner;

public class Client extends Thread{
    private DatagramSocket socket;

    public Client() {
        super("客户端：");
    }

    @Override
    public void run() {
            try {
                //////////////////////////发送数据////////////////////////////////////////
            socket = new DatagramSocket();
            byte[] b = new byte[1024];
            DatagramPacket dp2 = new DatagramPacket(b,b.length);
        while(true) {
            Scanner sc = new Scanner(System.in);
            String strClientMsg = this.getName();
             strClientMsg += sc.next();
            DatagramPacket dp = new DatagramPacket(strClientMsg.getBytes(),strClientMsg.getBytes().length,
                    new InetSocketAddress("127.0.0.1",8899));
            System.out.println(strClientMsg);
            socket.send(dp);

            //////////////////////////////////////接收数据///////////////////////////////////////////
            socket.receive(dp2);
            int len = dp2.getLength();
            String strPsg = new String(b,0,len);
            System.out.println(strPsg);
        }
        } catch (Exception e) {
                e.printStackTrace();
            }

    }
}
